uniform float speed;
uniform vec4 color;

float rand(vec2 n) {
  return fract(sin(dot(n, vec2(12.9898,12.1414))) * 83758.5453);
}

float noise(vec2 n) {
  const vec2 d = vec2(0.0, 1.0);
  vec2 b = floor(n);
  vec2 f = mix(vec2(0.0), vec2(1.0), fract(n));
  return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);   
}

vec3 ramp(float t) {
  
  return t <= .5 ? vec3( 1. - t * 1.4, .2, 1.05 ) / t : vec3( .3 * (1. - t) * 2., .2, 1.05 ) / t;
}

float fire(vec2 n) {
  return noise(n) + noise(n * 2.1) * .6 + noise(n * 5.4) * .42;
}

vec3 getLine(vec3 col, vec2 fc, mat2 mtx, float shift){
  float iTime = czm_frameNumber * speed / 1000.;
  float t = iTime;
  vec2 uv = (fc / 1.0) * mtx;
  
  uv.x += uv.y < .5 ? 23.0 + t * .35 : -11.0 + t * .3;    
  uv.y = abs(uv.y - shift);
  uv *= 5.0;
  
  float q = fire(uv - t * .013) / 2.0;
  vec2 r = vec2(fire(uv + q / 2.0 + t - uv.x - uv.y), fire(uv + q - t));
  vec3 color = vec3((vec3(0.5, 0.0, .1)));
  
  float grad = pow((r.y + r.y) * max(.0, uv.y) + .1, 4.0);
  color = ramp(grad);
  color /= (1.50 + max(vec3(0), color));
  
  return mix(col, color, color.b);
}

czm_material czm_getMaterial(czm_materialInput materialInput){
  czm_material material = czm_getDefaultMaterial(materialInput);
  vec2 v_st = materialInput.st; // 获取uv坐标
  
  vec2 uv = v_st;
  float iTime = czm_frameNumber * speed / 1000.;
  
  vec3 color2 = vec3(0);
  color2 = getLine(color2, uv, mat2(1., 1., 0., 1.), 1.02);
  color2 = getLine(color2, uv, mat2(1., 1., 1., 0.), 1.02);
  color2 = getLine(color2, uv, mat2(1., 1., 0., 1.), -0.02);
  color2 = getLine(color2, uv, mat2(1., 1., 1., 0.), -0.02);

  vec4 fragColor = vec4( color2 * color.rgb, 1.);


  material.diffuse = fragColor.rgb;
  material.alpha = pow(color2.r ,1.) * pow(color2.g ,1.) * pow(color2.b ,1.) * color.a;
  
  return material;
}